package net.diaowen.dwsurvey.controller;



import net.diaowen.common.base.entity.User;
import net.diaowen.common.utils.HttpRequest;
import net.diaowen.dwsurvey.entity.Torg;
import net.diaowen.dwsurvey.service.UserManager;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import net.diaowen.dwsurvey.service.TorgManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author  joker
 * @date  ${DATE} ${TIME}
 * @version 1.0
 */
@Controller
@RequestMapping("/Insrole")
public class InstitutionalRoleController {

    @Autowired
    private TorgManager torgManager;

    @Autowired
    private UserManager userManager;

    @RequestMapping("/edit.do")
    public ModelAndView doit(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/diaowen-answer/insrole");
        System.out.println("调用insrole");
        System.out.println(modelAndView);
        return modelAndView;
    }
    @RequestMapping("/selectall.do")
    @ResponseBody
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
        try{
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/json; charset=utf-8");
            List<Torg> list=torgManager.selectall();
            JSONArray jsonArray=new JSONArray(list);
            response.getWriter().write(jsonArray.toString());
        }
        catch (Exception e){
            e.printStackTrace();
        }
//        Iterator iterator=list.iterator();
//        while (iterator.hasNext()){
//            System.out.println("输出遍历结果"+iterator.next());
//        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/diaowen-answer/insrole");
        System.out.println("123456");
        System.out.println(modelAndView);

    }
//    查询机构管理机构中的用户做显示
    @RequestMapping("/selectuser.do")
    public ModelAndView selectuser(HttpServletRequest request, HttpServletResponse response) throws Exception{

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");
        String id=request.getParameter("id");
        List<User> list=userManager.selectorgid(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/diaowen-answer/UserShow");
        modelAndView.addObject("userlist",list);
        modelAndView.addObject("id",id);

        System.out.println(modelAndView);
        return modelAndView;

    }
//    创建机构名称
    @RequestMapping("/createorganization.do")
    @ResponseBody
    public void createorganization(HttpServletRequest request,HttpServletResponse response)throws Exception{
            ModelAndView modelAndView = new ModelAndView();
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/json;charset=utf-8");
            int id= Integer.parseInt(request.getParameter("id"));
            String orgname=request.getParameter("orgname");
            int result=torgManager.setorgname(id,orgname);
            List list=new ArrayList();
            list.add(result);
            JSONArray jsonArray=new JSONArray(list);
            response.getWriter().write(jsonArray.toString());
//            modelAndView.setViewName("/diaowen-lottery/list");
//            return jsonArray.toString();
        }
        catch (Exception e){
            response.getWriter().write("[0]");
            System.out.println("插入失败");
            e.printStackTrace();
        }
//        return new JSONArray(new ArrayList()).toString();
    }
//按email和name删除用户
    @RequestMapping("deleteuser.do")
    public ModelAndView deleteuser(HttpServletRequest request,HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");
        String email=request.getParameter("email");
        String name=request.getParameter("name");
//delete语句
        try{
//            delete操作
//            int result=userManager.deleteuser(email,name);
            int result=0;
            System.out.println("输出一下result"+result);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/diaowen-answer/zhongzhuanye");
        System.out.println("调用deleteuser");
        System.out.println(modelAndView);
        return modelAndView;

    }

//    text页面
    @RequestMapping("/test.do")
    public ModelAndView test(HttpServletRequest request,HttpServletResponse response){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/diaowen-organizational/yxlwenjuan");
        return modelAndView;
    }


}
